package communicationMessages;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;


import logger.LoggerWriter;
import database.DB;

public class ViewThreadMessage extends Message {
	final String delimiter2 =(char)2+"";
	@Override
	public String getAnswer() {
		String ans = "postList"+delimiter;
		LoggerWriter logger  = LoggerWriter.getLoggerWriter();
		String arr[] = this.content.split(delimiter);
		if (arr.length != 3)
		{
			logger.log("wrong format ViewSubForumMessage",Level.WARNING);
			ans = "wrong format ViewSubForumMessage"+delimiter;
		}
		else
		{
			String userName = arr[0];
			String subForum = arr[1];
			String thread = arr[2];
			if (!DB.isSubForumExist(subForum))
			{
				System.out.println("subforum is null");
				System.out.println("the number is " + subForum + delimiter + subForum);
				return ans="pathNotFound" + delimiter;
			}
			
			if (DB.isThreadExist(thread))
			{
				try
				{
					Vector<String> path = new Vector<String>();
					ans = getUserRoll(ans, userName, subForum, thread, path);
					ans = getThread(ans, thread, path, userName);
					
					ans = getPostList(ans, subForum, thread, path, userName);
					changeCurrentPath(-1, Thread.currentThread().getId());
					changeCurrentPathViewThread(Integer.parseInt(thread), Thread.currentThread().getId());
				}
				catch (SQLException e) {
					System.out.println("in view thread SQLException " + e.getMessage());
				}
			}
			else
			{
				ans = "pathNotFound" + delimiter;  
			}
		}
		return ans;
	}
	
	private String getUserRoll(String ans, String userName, String subForum,String thread, Vector<String> path) {
		path.add(subForum);
		path.add(thread);
		ans += DB.getUserRoll(userName,path)+delimiter;
		return ans;
	}
	
	private String getThread(String ans, String thread, Vector<String> path,
			String userName) throws SQLException {
		ResultSet res = DB.getThread(thread);
		res.next();
		ans+= res.getString(DB.threads.threadId.ordinal()) + ":" + res.getString(DB.threads.threadTitle.ordinal()) +
			delimiter2 +   DB.getUserRoll(userName,path)+delimiter2 + res.getString(DB.threads.threadContent.ordinal()) + delimiter;
		return ans;
	}
	
	private String getPostList(String ans, String subForum, String thread,Vector<String> path, String userName)
			throws SQLException 
	{
		ResultSet postListRes = DB.getPostList(thread);
		while (postListRes.next()) 
		{
			String postID = postListRes.getString(DB.posts.postsId.ordinal());
			path.clear();// = new Vector<String>();
			path.add(subForum);
			path.add(thread);
			path.add(postID);
			String postTitle = postListRes.getString(DB.posts.postitle.ordinal());
			String postContent = postListRes.getString(DB.posts.postContent.ordinal());
			ans+=postID + ":" + postTitle + delimiter2 +
			DB.getUserRoll(userName,path)+delimiter2+ postContent + delimiter;
			
		}
		return ans;
	}
	
//	  else
//      {
//              String subForum = arr[1];
//              Integer subForumInteger = Integer.valueOf(subForum);
//              SubForum tSubForum = Forum.getForum().getSubForum(subForumInteger);
//              if (tSubForum == null)
//              {
//                      System.out.println("subforum is null");
//                      System.out.println("the number is " + subForum + delimiter + subForumInteger);
//                      return ans;
//              }
//              Integer threadInteger = Integer.valueOf(arr[2]);
//              ThreadMessage tThraedMsgMessage = tSubForum.getThreadMessage(threadInteger);
//              if (tThraedMsgMessage != null)
//              {
//                      Vector<Integer> path = new Vector<Integer>();
//                      path.add(subForumInteger);
//                      path.add(threadInteger);
//                      String userName = arr[0];
//                      ans += MemberDataBase.getUserRoll(userName,path)+delimiter;
//                      
//                      ans+=tThraedMsgMessage.get_id() + ":" + tThraedMsgMessage.get_title() + 
//                      delimiter2 +MemberDataBase.getUserRoll(userName,path)+delimiter2        + tThraedMsgMessage.get_content() + delimiter;
//                      for (Entry<Integer, PostMessage> tEntry : tThraedMsgMessage.getResponse().entrySet()) 
//                      {
//                              PostMessage tPostMsg = tEntry.getValue();
//                              path = new Vector<Integer>();
//                              path.add(subForumInteger);
//                              path.add(tPostMsg.get_id());
//                              ans+=tPostMsg.get_id() + ":" + tPostMsg.get_title() + delimiter2 +
//                              MemberDataBase.getUserRoll(userName,path)+delimiter2+ tPostMsg.get_content() + delimiter;
//                              
//                      }
//              }
//              else
//              {
//                      ans = "pathNotFound" + delimiter;  
//              }
//      }

}
